options compress=YES;
libname manasi ''; 

* THIS PROGRAM CREATES ONE RECORD FOR EACH SSI CHILD-PARENT PAIR WITH ALL
OF THE PARENT'S RECORD START AND STOP DATES ;

* Create dataset with only stop and start dates of records;
data manasi.ssr_parents_estpdonly;
	set manasi.ssr_parents_full (keep=hun pan estpd stop_rd);
	if (pan='*00000000' | pan='' | pan='000000000') then delete;

	* Turn stop and start variables into dates;
	start_str=put(estpd,8.);
	start_yy=substr(start_str,1,4);
	start_mm=substr(start_str,5,2);
	start_dd=substr(start_str,7,2);
	start=mdy(start_mm, start_dd, start_yy);
	
	stop_str=put(stop_rd,6.);
	stop_yy=substr(stop_str,1,4);
	stop_mm=substr(stop_str,5,2);
	stop_dd='30';
	stop=mdy(stop_mm, stop_dd, stop_yy);
	
	drop stop_: start_: estpd;
	
run;

* Sort by PAN (parent), HUN (child), and record date;
proc sort data=manasi.ssr_parents_estpdonly out=manasi.ssr_parents_estpdonly;
	by pan hun start;
run;

* Create unique identifier for each parent-child pair and merge into estpdonly;
data manasi.ssr_parents_panhunid;
	set manasi.ssr_parents_full(keep=hun pan);
	if (pan='*00000000' | pan='' | pan='000000000') then delete;
run;

proc sort data=manasi.ssr_parents_panhunid out=manasi.ssr_parents_panhunid nodupkey;
	by pan hun;
run;

data manasi.ssr_parents_panhunid;
	set manasi.ssr_parents_panhunid;
	panhun=_N_;
run;

data manasi.ssr_parents_estpdonly;
	merge
		manasi.ssr_parents_estpdonly
		manasi.ssr_parents_panhunid;
	by pan hun;
run;

* Create ID variable for each record for each parent-child pair;
data manasi.ssr_parents_estpdonly_wide;
	set manasi.ssr_parents_estpdonly;
	recno + 1;
	by panhun;
	if first.panhun then recno=1;
run;

* Find out how many records per parent-kid pair;
proc means data=manasi.ssr_parents_estpdonly_wide noprint;
	by panhun;
	var recno;
	output out=manasi.ssr_parents_estpdonly_stats max(recno)=max_recno;
run;

proc means mean median min max p75 p90 p95 p99 data=manasi.ssr_parents_estpdonly_stats;
	var max_recno;
run;

data manasi.ssr_parents_estpdonly_wide;
	merge 	
			manasi.ssr_parents_estpdonly_wide 
			manasi.ssr_parents_estpdonly_stats
				(keep = panhun max_recno);
	by panhun; 
run;

data manasi.ssr_parents_estpdonly_wide;
	set manasi.ssr_parents_estpdonly_wide;
	if recno<=7;
run;

* Reshape wide;
data manasi.ssr_parents_estpdonly_wide;
	set manasi.ssr_parents_estpdonly_wide;
	by panhun;

	keep 	panhun 
			pan
			hun
			start1-start7
			stop1-stop7
			;	

	retain 	start1-start7
			stop1-stop7
			;

	array 	astart(1:7) 	start1-start7 ; 
	array 	astop(1:7)		stop1-stop7;	

	if first.panhun then
	do;
		do i=1 to 7;
			astart(i)=.;
			astop(i)=.;
		end;
	end;

	astart(recno) = start ;	
	astop(recno) = stop;

	if last.panhun then output;

run;

* Create dummies for whether parent is in household in year X;
data manasi.ssr_parents_estpdonly_wide;
	set manasi.ssr_parents_estpdonly_wide;

	format stop: start: date9.;

	inhh2002=0;
	inhh2003=0;
	inhh2004=0;
	inhh2005=0;
	inhh2006=0;

	%macro inhh(num, yr);
	if 		year(start&num.)<=&yr. & 
			year(start&num.)^=. & 
			(year(stop&num.)>=&yr. | year(stop&num.)=.)
 
	then 	inhh&yr.=1;
	%mend inhh;

	%inhh(1,2002); %inhh(2,2002); %inhh(3,2002); %inhh(4,2002); %inhh(5,2002); %inhh(6,2002); %inhh(7,2002);
	%inhh(1,2003); %inhh(2,2003); %inhh(3,2003); %inhh(4,2003); %inhh(5,2003); %inhh(6,2003); %inhh(7,2003);
	%inhh(1,2004); %inhh(2,2004); %inhh(3,2004); %inhh(4,2004); %inhh(5,2004); %inhh(6,2004); %inhh(7,2004);
	%inhh(1,2005); %inhh(2,2005); %inhh(3,2005); %inhh(4,2005); %inhh(5,2005); %inhh(6,2005); %inhh(7,2005);
	%inhh(1,2006); %inhh(2,2006); %inhh(3,2006); %inhh(4,2006); %inhh(5,2006); %inhh(6,2006); %inhh(7,2006);

	if 	(inhh2002=1 | inhh2003=1 | inhh2004=1 | inhh2005=1 | inhh2006=1)
		then 	inhh0206=1;
		else inhh0206=0;

run;

* Sort by child so you can see if kids have parents coming and going;
proc sort data=manasi.ssr_parents_estpdonly_wide out=manasi.ssr_parents_estpdonly_wide;
	by hun pan;
run;

